package com.example.usermanage.configuration;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@ControllerAdvice
@ResponseBody
public class ErrorAdvice {
    @ExceptionHandler(NullPointerException.class)
    //拦截什么异常就写什么异常
    public HashMap<String,Object> errorAdvice(NullPointerException e){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("success",200);
        hashMap.put("state",-1);
        hashMap.put("message",e.toString());
        return hashMap;
    }
    @ExceptionHandler(Exception.class)
    //拦截什么异常就写什么异常
    public HashMap<String,Object> exception(Exception e){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("success",200);
        hashMap.put("state",-1);
        hashMap.put("message",e.toString());
        return hashMap;
    }
}
